package com.fgr.controller;

import com.fgr.beans.Help;
import com.fgr.beans.User;
import com.fgr.service.HelpService;
import com.fgr.service.Impl.HelpServiceImpl;
import com.fgr.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

@Controller
public class HelpController {
    @Autowired
    HttpSession session;
    @Autowired
    HelpService helpService;
    @Autowired
    UserService userService;

    @PostMapping("AddHelp")
    @ResponseBody
    public String AddHelp(Help help,HttpSession httpSession) {
        String s = helpService.AddHelp(help,httpSession);
        return s;
    }

    @PostMapping("DetermineLoginHelp")
    @ResponseBody
    public String DetermineLoginHelp(HttpSession httpSession){
        Object obj = httpSession.getAttribute("userid");
        if (obj==null){
            return "Login";
        }
        return "SUCCESS";

    }

    @GetMapping("findAllHelpStrayBg")
    public String findAllHelpStrayBg(Model model){
        List<Help> allHelpStrayBg = helpService.findAllHelpStrayBg();
        model.addAttribute("allHelpStrayBg",allHelpStrayBg);
        return "background/help-stray";
    }

    @GetMapping("findHelpByIdAndUser")
    public String findHelpByIdAndUser(Integer hId,Model model){
        Help help = helpService.findHelpByHid(hId);
        List<User> users = userService.selectAllUser();
        model.addAttribute("help",help);
        model.addAttribute("users",users);
        return "background/modifyhelp";

    }

    @ResponseBody
    @PostMapping("addHelpBg")
    public String addHelpBg(Help help){
        String s = helpService.addHelpBg(help);
        return s;

    }

    @PostMapping("deleteHelpByhIdBg")
    @ResponseBody
    public String deleteHelpByhIdBg(Integer hId){
        String s = helpService.deleteHelpByhIdBg(hId);
        return s;

    }
    @PostMapping("updateHelpBg")
    @ResponseBody
    public String updateHelpBg(Help help){
        String s = helpService.updateHelpBg(help);
        return s;
    }


}